import os
import logging
import csv 
from configparser import ConfigParser
from logger import setup_logger
from data_collector import DataCollector
import time
from pathlib import Path
def save_to_csv(data, filename=r"raw_data.csv"):
    """将数据保存到CSV文件"""
    try:
        # 定义CSV文件的列名
        fieldnames = [
            'id', 'text', 'user', 'reposts', 'comments', 'likes',
            'timestamp', 'platform', 'sentiment', 'keywords',
            'alert_type', 'risk_label', 'police_terms', 'context_snippet'
        ]
        
        # 确保目录存在
        os.makedirs(os.path.dirname(filename), exist_ok=True)
        
        # 写入CSV文件
        with open(filename, 'w', newline='', encoding='utf-8-sig') as csvfile:
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            writer.writeheader()
            
            for post in data:
                # 准备数据行
                row = {
                    'id': str(post.get('id', '')),
                    'text': str(post.get('text', '')),
                    'user': str(post.get('user', '')),
                    'reposts': int(post.get('reposts', 0)),
                    'comments': int(post.get('comments', 0)),
                    'likes': int(post.get('likes', 0)),
                    'timestamp': str(post.get('timestamp', '')),
                    'platform': str(post.get('platform', 'weibo')),
                    'sentiment': float(post.get('sentiment', 0.5)),
                    'keywords': ','.join(map(str, post.get('keywords', []))),
                    'alert_type': str(post.get('alert_type', {}).get('type', '其他')),
                    'risk_label': int(post.get('alert_type', {}).get('risk_level', 0)),
                    'police_terms': ','.join(post.get('alert_type', {}).get('keywords', [])),
                    'context_snippet': str(post.get('alert_type', {}).get('context', ''))[:200]
                }
                writer.writerow(row)
        
        logging.info(f"成功存储 {len(data)} 条数据到CSV文件 {filename}")
        return True
    except Exception as e:
        logging.error(f"CSV存储失败: {str(e)}")
        return False
    
if __name__ == '__main__':

    logger = setup_logger()
    config_path = Path(__file__).parent / "config.ini"  # 配置文件与脚本同目录
    with open(config_path, 'r', encoding='utf-8') as f:
      print(f.read())  # 检查是否与磁盘文件一致
    print(f"配置文件路径: {config_path.resolve()}")  # 解析为绝对路径
    config = ConfigParser()
    config.read(config_path, encoding='utf-8')  # ✅ 使用Path对象传递绝对路径
    print("已加载配置节:", config.sections())  # 打印所有节名

    if 'weibo' not in config:
        raise ValueError("配置文件中缺失 [weibo] 节！")
    breakpoint()
    collector = DataCollector(cookie=config['weibo']['cookie'])
    # ================= 数据采集 =================
    logger.info("开始采集微博数据...")
    raw_posts = []
    for attempt in range(3):
        try:
            raw_posts = collector.crawl_weibo(
                keyword=config['weibo']['keyword'],
                pages=int(config['weibo']['pages'])
            )
            if raw_posts:
                logger.info(f"成功采集{len(raw_posts)}条原始数据")
                break
            logger.warning(f"第{attempt+1}次采集返回空数据")
        except Exception as e:
            logger.warning(f"采集失败: {str(e)}")
            time.sleep(2)
    # 保存原始数据
    save_to_csv(raw_posts)
